import Mock from 'mockjs'

// get请求从config.url获取参数，post从config.body中获取参数
function param2Obj(url) {
    const search = url.split('?')[1]
    if (!search) {
        return {}
    }
    return JSON.parse(
        '{"' +
        decodeURIComponent(search)
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"') +
        '"}'
    )
}

Date.prototype.format = function (fmt) {
    var o = {
      "M+": this.getMonth() + 1, //月份
      "d+": this.getDate(), //日
      "h+": this.getHours(), //小时
      "m+": this.getMinutes(), //分
      "s+": this.getSeconds(), //秒
      "q+": Math.floor((this.getMonth() + 3) / 3), //季度
      S: this.getMilliseconds(), //毫秒
    };
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length)
      );
    }
    for (var k in o) {
      if (new RegExp("(" + k + ")").test(fmt)) {
        fmt = fmt.replace(
          RegExp.$1,
          RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
        );
      }
    }
    return fmt;
  };

let List = []
List.push({
    id: 1,
    title: "中期检查",
    activityContext: "去学校指定教室中期进行中期检查",
    deadline: new Date(2021, 5, 1).format("yyyy-MM-dd"),
    completed: 1,
    type: 2,
})
const count = 90

for (let i = 0; i < count; i++) {
    List.push(
        Mock.mock({
            id: Mock.Random.guid(),
            title: Mock.Random.ctitle(3),
            activityContext: Mock.Random.csentence(5, 10),
            deadline: Mock.Random.date(),
            completed: Mock.Random.integer(0, 1),
            type: Mock.Random.integer(0, 4)
        })
    )
}

export default {
    getActivityList: config => {
        const {
            title,
            page = 1,
            limit = 20
        } = param2Obj(config.url)
        console.log('title:' + title, 'page:' + page, '分页大小limit' + limit)
        const mockList = List.filter(activity => {
            if (title && activity.title.indexOf(title) === -1 && activity.activityContext.indexOf(title) == -1) return false
            return true
        })
        const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
        return {
            code: 20000,
            count: mockList.length,
            list: pageList
        }
    },
    createActivity: config => {
        const {
            title,
            activityContext,
            deadline, 
            completed,
            type
        } = JSON.parse(config.body)
        console.log(JSON.parse(config.body))
        List.unshift({
            id: Mock.Random.guid(),
            title: title,
            activityContext: activityContext,
            deadline: deadline,
            completed: completed,
            type: type
        })
        return {
            code: 20000,
            data: {
                message: '添加成功'
            }
        }
    },
    deleteActivity: config => {
        const {
            id
        } = param2Obj(config.url)
        if (!id) {
            return {
                code: -999,
                message: '参数不正确'
            }
        } else {
            List = List.filter(at => at.id !== id)
            return {
                code: 20000,
                message: '删除成功'
            }
        }
    },
    batchremove: config => {
        let {
            ids
        } = JSON.parse(config.body)
        List = List.filter(at => !ids.includes(at.id))
        return {
            code: 20000,
            data: {
                message: '批量删除成功'
            }
        }
    },
    updateActivity: config => {
        const {
            id,
            title,
            activityContext,
            deadline,
            completed,
            type,
        } = JSON.parse(config.body)
        List.some(at => {
            if (at.id === id) {
                at.title = title
                at.activityContext = activityContext
                at.deadline = deadline
                at.completed = completed
                at.type = type
                return true
            }
        })
        return {
            code: 20000,
            data: {
                message: '编辑成功'
            }
        }
    }
}